import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
// import 'package:mini_bmob/mini_bmob.dart';

class ForgetPwdViewModel extends GetxController {
  final TextEditingController phoneController = TextEditingController();
  final TextEditingController codeController = TextEditingController();
  final TextEditingController pwdController = TextEditingController();
  final TextEditingController pwd1Controller = TextEditingController();

  final obscure = true.obs;

  final timeInterval = 0.obs;
  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
  }

  void sendCode() {
    if (phoneController.text.isEmpty) {
      Fluttertoast.showToast(msg: "请输入手机号");
      return;
    }

    if (timeInterval.value != 0) {
      return;
    }

    timeInterval.value = 60;
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      timeInterval.value -= 1;
      if (timeInterval.value == 0) {
        _timer!.cancel();
      }
    });
  }

  void registerClick() {
    if (phoneController.text.isEmpty) {
      Fluttertoast.showToast(msg: "请输入邮箱");
      return;
    }

    if (codeController.text.isEmpty) {
      Fluttertoast.showToast(msg: "请输入验证码");
      return;
    }

    if (pwdController.text.isEmpty) {
      Fluttertoast.showToast(msg: "请输入密码");
      return;
    }

    if (pwd1Controller.text.isEmpty) {
      Fluttertoast.showToast(msg: "两次密码不一致");
      return;
    }

    if (pwd1Controller.text != pwdController.text) {
      Fluttertoast.showToast(msg: "两次密码不一致");
      return;
    }

    {
      Fluttertoast.showToast(msg: "验证码不正确");
      return;
    }

    // BmobUserTable _user = BmobUserTable(
    //     username: codeController.text, password: pwdController.text);
    // _user.update().then((value) {
    //   if (value) {
    //     Fluttertoast.showToast(msg: "请登录");
    //     Get.back();
    //   } else {
    //     Fluttertoast.showToast(msg: "请求失败失败");
    //   }
    // });
  }

  @override
  void dispose() {
    super.dispose();

    if (_timer != null) {
      _timer!.cancel();
    }
  }
}
